package com.aimee.servlet;

import com.aimee.dao.LeaveWordDAO;
import com.aimee.entity.LeaveWordAnswers;
import com.aimee.entity.LeaveWords;
import com.aimee.entity.User;
import com.google.gson.Gson;

import javax.net.ssl.HandshakeCompletedEvent;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/admin/leaveword")
public class LeaverWordServlet extends BaseServlet {
    LeaveWordDAO dao = new LeaveWordDAO();


    //查找留言信息
    protected void findLeaveword(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        System.out.println("查找留言信息");
        HttpSession session = request.getSession();
        int user_id = Integer.parseInt(session.getAttribute("user_id").toString());
        System.out.println(user_id);
        List<LeaveWords> leaveWordlist = dao.findAllLeaveWords();//留言集合
        for (int i = 0; i < leaveWordlist.size(); i++) {
            List<LeaveWords> leaveuserlist = dao.findUserNameByuserid(leaveWordlist.get(i).user_id);
            leaveWordlist.get(i).setUser_name(leaveuserlist.get(0).user_name);
        }
        System.out.println("查找留言回复信息");
        List leaveWordAnswerlist = new ArrayList();//装有留言回复集合的集合
        for (int i = 0; i < leaveWordlist.size(); i++) {
            List<LeaveWordAnswers> list = dao.findLeaveWordAnswer(leaveWordlist.get(i).leaveWord_id);//某留言的所有回复集合
            for (int j = 0; j < list.size(); j++) {
                List<LeaveWordAnswers> answersuserlist = dao.findUserNameByansweruserid(list.get(j).user_id);
                list.get(j).setUser_name(answersuserlist.get(0).user_name);
            }
            leaveWordAnswerlist.add(list);
        }
        request.setAttribute("leaveWordlist", leaveWordlist);
        request.setAttribute("leaveWordAnswerlist", leaveWordAnswerlist);

        System.out.println("留言集合=" + leaveWordlist);
        System.out.println("回复的集合的集合" + leaveWordAnswerlist);
        for (int i = 0; i < leaveWordlist.size(); i++) {
            System.out.println("第" + i + "条留言：" + leaveWordlist.get(i));//第i条留言
            List<LeaveWordAnswers> l = (List<LeaveWordAnswers>) leaveWordAnswerlist.get(i);//第i条留言的所有回复集合
            System.out.println("第" + i + "条留言回复集合：" + l);
            for (int j = 0; j < l.size(); j++) {
                System.out.println("第" + i + "条留言回复的第" + j + "条回复id=" + l.get(j).answer_id);//第i条留言的所有回复的id
            }
//            System.out.println(((List<LeaveWordAnswers>) ((List<LeaveWordAnswers>) leaveWordAnswerlist.get(i))).get(0).answer_id);
        }
        //判断用户角色
        List<User> rolelist = dao.findrole(user_id);
        if (rolelist.get(0).getRole_id() == 1) {
            request.getRequestDispatcher("/admin/allLeave.jsp").forward(request, response);
        } else {
            request.getRequestDispatcher("/user/allLeave.jsp").forward(request, response);
        }

    }

    //添加留言
    protected void addLeaveword(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        System.out.println("添加留言信息");

        HttpSession session = request.getSession();
        int user_id = Integer.parseInt(session.getAttribute("user_id").toString());
        System.out.println("user_id");
        String leaveWord_content = request.getParameter("Content");
        leaveWord_content = new String(leaveWord_content.getBytes("ISO-8859-1"), "utf-8");
        System.out.println("从jsp获取的留言内容：" + leaveWord_content);
        LeaveWords leaveWords = new LeaveWords(leaveWord_content, user_id);
        dao.addleaveword(leaveWords);
        response.sendRedirect("http://localhost:8080/OnlineExam/admin/leaveword?m=findLeaveword");

    }

    //添加回复
    protected void addleavewordanswer(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        System.out.println("添加回复信息");
        HttpSession session = request.getSession();
        int user_id = Integer.parseInt(session.getAttribute("user_id").toString());
        System.out.println("user_id");
        int leaveWord_id = Integer.parseInt(request.getParameter("leaveWord_id"));
        String answer_content = request.getParameter("Content");
        answer_content = new String(answer_content.getBytes("ISO-8859-1"), "utf-8");
        System.out.println("从jsp获取的回复内容：" + answer_content);
        LeaveWordAnswers leaveWordAnswers = new LeaveWordAnswers(leaveWord_id, answer_content, user_id);
        dao.addleavewordanswer(leaveWordAnswers);
        response.sendRedirect("http://localhost:8080/OnlineExam/admin/leaveword?m=findLeaveword");

    }

    //删除留言以及留言回复
    protected void delleaveword(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("删除留言以及留言回复");
        int leaveWord_id = Integer.parseInt(request.getParameter("leaveWord_id"));
        try {
            dao.delleaveword(leaveWord_id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //去哪？去查第一页
        response.sendRedirect("http://localhost:8080/OnlineExam/admin/leaveword?m=findLeaveword");

    }

    //删除留言回复
    protected void delleavewordanswer(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("删除留言回复");
        int answer_id = Integer.parseInt(request.getParameter("answer_id"));
        try {
            dao.delleavewordanswer(answer_id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //去哪？去查第一页
        response.sendRedirect("http://localhost:8080/OnlineExam/admin/leaveword?m=findLeaveword");

    }

    //查找留言回复信息 http://localhost:8080/OnlineExam/admin/leaveword?m=findLeaveWordAnswer&leaveWord_id=1  leaveWord_id为传过来的值
/*    protected void findLeaveWordAnswer(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        System.out.println("查找留言回复信息");

        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        PrintWriter out = response.getWriter();
        response.setContentType("text/html;charset=UTF-8");

        Gson gson = new Gson();

        int leaveWord_id = Integer.parseInt(request.getParameter("leaveWord_id"));
        System.out.println("接受ajax请求"+leaveWord_id);

        List<LeaveWordAnswers> leaveWordAnswerlist  = dao.findLeaveWordAnswer(leaveWord_id);
        if (leaveWordAnswerlist == null) {
            System.out.println("此留言无回复");
            request.getRequestDispatcher("leaveword?m=findLeaveword").forward(request, response);
        }
        for (int i = 0; i <leaveWordAnswerlist.size() ; i++) {
            int user_id=leaveWordAnswerlist.get(i).user_id;
            List<LeaveWordAnswers> usernameList=dao.findUserNameByansweruserid(user_id);
            leaveWordAnswerlist.get(i).setUser_name(usernameList.get(0).user_name);
        }
        System.out.println(leaveWordAnswerlist);

        String str = gson.toJson(leaveWordAnswerlist);
        out.print(str);
        out.flush();
        out.close();
    }*/


    //    *************************************我的留言******************************************************
//    查找我的留言

    protected void findmyleave(HttpServletRequest request, HttpServletResponse response) throws Exception {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        PrintWriter out = response.getWriter();
        response.setContentType("text/html;charset=UTF-8");
        Gson gson = new Gson();

        HttpSession session = request.getSession();
        int user_id = Integer.parseInt(session.getAttribute("user_id").toString());
        System.out.println("user_id");

        System.out.println("查找我的留言");
        List<LeaveWords> myleavelist = dao.findmyleave(user_id);
        request.setAttribute("myleavelist", myleavelist);


      /*  System.out.println("查找我的回复");
        List<LeaveWordAnswers> myleaveanswerlist = dao.findmyanswer(user_id);
        request.setAttribute("myleaveanswerlist", myleaveanswerlist);
*/
    /*    System.out.println("查询对我的回复");
        List<LeaveWordAnswers> tomeanswerlist = dao.findtomeanswer(user_id);
        request.setAttribute("tomeanswerlist", tomeanswerlist);

        request.getRequestDispatcher("/admin/myLeave.jsp").forward(request, response);*/
        int count = (int) dao.countmyleave(user_id);
        String list = gson.toJson(myleavelist);
        String res = "{\n" +
                "  \"code\": 0,\n" +
                "  \"msg\": \"\",\n" +
                "  \"count\":" + count + ",\n" +
                "  \"data\":" + list + "\n" +
                "} ";
        System.out.println(res);
        out.print(res);
        out.flush();
        out.close();
    }

    //查找我的回复
    protected void findmyanswer(HttpServletRequest request, HttpServletResponse response) throws Exception {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        PrintWriter out = response.getWriter();
        response.setContentType("text/html;charset=UTF-8");
        Gson gson = new Gson();

        HttpSession session = request.getSession();
        int user_id = Integer.parseInt(session.getAttribute("user_id").toString());
        System.out.println("user_id");

        System.out.println("查找我的回复");
        List<LeaveWordAnswers> myleaveanswerlist = dao.findmyanswer(user_id);
        request.setAttribute("myleaveanswerlist", myleaveanswerlist);
        int count = (int) dao.countmyanswer(user_id);

        String list = gson.toJson(myleaveanswerlist);
        String res = "{\n" +
                "  \"code\": 0,\n" +
                "  \"msg\": \"\",\n" +
                "  \"count\":" + count + ",\n" +
                "  \"data\":" + list + "\n" +
                "} ";
        System.out.println(res);
        out.print(res);
        out.flush();
        out.close();

    }
//查找对我的回复
protected void findtomeanswer(HttpServletRequest request, HttpServletResponse response) throws Exception {
    request.setCharacterEncoding("utf-8");
    response.setCharacterEncoding("utf-8");
    PrintWriter out = response.getWriter();
    response.setContentType("text/html;charset=UTF-8");
    Gson gson = new Gson();

    HttpSession session = request.getSession();
    int user_id = Integer.parseInt(session.getAttribute("user_id").toString());
    System.out.println("user_id");

    System.out.println("查询对我的回复");
    List<LeaveWordAnswers> tomeanswerlist = dao.findtomeanswer(user_id);
    request.setAttribute("tomeanswerlist", tomeanswerlist);
    int count = (int) dao.counttomeanswer(user_id);
    String list = gson.toJson(tomeanswerlist);
    String res = "{\n" +
            "  \"code\": 0,\n" +
            "  \"msg\": \"\",\n" +
            "  \"count\":" + count + ",\n" +
            "  \"data\":" + list + "\n" +
            "} ";
    System.out.println(res);
    out.print(res);
    out.flush();
    out.close();

}
    //删除我的留言通过留言id
    protected void delmyleave(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("删除我的留言通过留言id");
        int leaveWord_id = Integer.parseInt(request.getParameter("leaveWord_id"));
        try {
            dao.delmyleave(leaveWord_id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //去哪？去查第一页
        response.sendRedirect("http://localhost:8080/OnlineExam/admin/myLeave.jsp");
//        request.getRequestDispatcher("/admin/myLeave.jsp").forward(request, response);
    }

    //删除我的回复 通过回复id
    protected void delmyanswer(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("删除我的回复 通过回复id");
        int answer_id = Integer.parseInt(request.getParameter("answer_id"));
        try {
            dao.delmyanswer(answer_id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //去哪？去查第一页
        response.sendRedirect("http://localhost:8080/OnlineExam/admin/myLeave.jsp");
//        request.getRequestDispatcher("/admin/myLeave.jsp").forward(request, response);
    }
}
